//
//  AfanHomeController.swift
//  weiboOfAfan
//
//  Created by afan on 16/3/19.
//  Copyright © 2016年 阿范. All rights reserved.
//

import UIKit

class AfanHomeController: AfanBaseController {

    override func viewDidLoad() {
        super.viewDidLoad()
        if !AfanUserAccountViewModel.shardInstace.userLogin{
            return
        }
        setupNavigationBar()
        setupTitleButton()
        
    }
    private func setupNavigationBar() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: "didClickLeftItem")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "didClickRightItem")
    }

    
    /// 设置导航栏title
    private func setupTitleButton() {
        
        //网络加载用户名
        let name =  AfanUserAccountViewModel.shardInstace.userAccount?.screen_name
        
        let titleButton = AfanHomeTitleButton(imageName: "navigationbar_arrow_down", title: name)
        titleButton.addTarget(self, action: "didClickTitleButton:", forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = titleButton
    }
    
    
    @objc private func didClickTitleButton(button: AfanHomeTitleButton) {
        button.selected = !button.selected

        UIView.animateWithDuration(0.3) { () -> Void in
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
        }
        if button.selected{
            let popoverVC = AfanPopoverViewController()
            // iOS8 以后 popoverPresentationController
            
            // 设置modal的显示样式
            popoverVC.modalPresentationStyle = .Popover
            
            // 设置popoverPresentationController的代理,实现代理的方法,来让popover不要适配iPhone
            popoverVC.popoverPresentationController?.delegate = self
            
            // popover的箭头指向titeView,默认指向左上角
            popoverVC.popoverPresentationController?.sourceView = navigationItem.titleView
            // 设置sourceView.bounds给sourceRect
            popoverVC.popoverPresentationController?.sourceRect = navigationItem.titleView!.bounds
            // 设置popover显示的大小
            popoverVC.preferredContentSize = CGSize(width: 200, height: 44 * popoverVC.titles.count)
            
            // 弹出
            presentViewController(popoverVC, animated: true, completion: nil)
        }
    }
    
    //MARK: 左右 Item 点击事件
    func didClickLeftItem() {
        print("didClickLeftItem")
    }
    
    func didClickRightItem() {
        print("didClickRightItem")
    }
    
}
extension AfanHomeController: UIPopoverPresentationControllerDelegate{
    // 不让popover适配iPhone,默认适配.全屏的,不适配就是popover的效果
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
    
    // 系统会询问我们是否需要关闭popover
    func popoverPresentationControllerShouldDismissPopover(popoverPresentationController: UIPopoverPresentationController) -> Bool {
        // 让箭头转下来
        let titleButton = navigationItem.titleView as! AfanHomeTitleButton
        didClickTitleButton(titleButton)
        
        return true
    }

}



